package com.code.config.source;

import com.code.config.DataSourceInfo;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public final class DatasourceFactory {

    private DatasourceFactory() {

    }

    public static DataSource getDataSource() {
        return DataSourceHolder.INSTANCE.getDataSource();
    }

    private enum DataSourceHolder {
        INSTANCE;
        private DataSource dataSource;

        DataSourceHolder() {
            HikariConfig hikariConfig = new HikariConfig();
            hikariConfig.setJdbcUrl(DataSourceInfo.URL.getValue());
            hikariConfig.setDriverClassName(DataSourceInfo.getDriver());
            hikariConfig.setUsername(DataSourceInfo.USERNAME.getValue());
            hikariConfig.setPassword(DataSourceInfo.PASSWORD.getValue());
            hikariConfig.setMaximumPoolSize(20);
            hikariConfig.setMinimumIdle(10);
            this.dataSource = new HikariDataSource(hikariConfig);
        }

        public DataSource getDataSource() {
            return dataSource;
        }
    }
}
